package com.longtop.framework.sys.menu.web;

import java.util.ArrayList;
import java.util.List;

import com.longtop.framework.base.BaseAction;
import com.longtop.framework.common.QueryConditions;
import com.longtop.framework.common.QueryResult;
import com.longtop.framework.security.SessionObject;
import com.longtop.framework.sys.menu.model.TSysMenu;
import com.longtop.framework.sys.menu.service.MenuService;
import com.longtop.framework.util.BeanUtil;
import com.longtop.framework.util.WebUtil;
import com.longtop.intelliweb.action.IActionContext;
import com.longtop.intelliweb.action.IInitContext;
import com.longtop.intelliweb.view.Dataset;

/**
 * 系统管理-机构
 * 
 * @author 杨向阳
 * 
 */
public class MenuDropdownAction extends BaseAction {

	private MenuService service;

	private final String DATASET_MENU = "Dataset_Menu";

	public MenuDropdownAction() {
		this.service = this.getBean(MenuService.BEAN_ID);
	}

	/**
	 * 系统共用：初始化调用
	 */
	public void init(IInitContext context) {
		super.init(context);
	}

	/**
	 * 本页jsp：初始化业务数据(覆写父类方法)
	 */
	public void thisPageInit(IInitContext context) {
		commonQuery(context, true);
	}

	/**
	 * 加载
	 * 
	 * @param context
	 */
	public void loadMenu(IActionContext context) {
		commonQuery(context, false);
	}

	/**
	 * private:读取菜单
	 * 
	 * @param context
	 * @param isRoot
	 *            是否延迟加载
	 */
	private void commonQuery(Object context, boolean isRoot) {
		Dataset dataset = this.getDataset(context, DATASET_MENU);
		if (!WebUtil.validateDatasetExist(this, dataset, DATASET_MENU)) {
			return;
		}
		QueryConditions conditions = WebUtil.createQueryConditions();
		if (isRoot) {
			conditions.setConditionIsNull("parentMenuId");
		}
		String filter = dataset.getFilter();
		QueryResult queryResult = service.loadMenu(conditions, filter);
		SessionObject sessionObject = this.getSessionObject(context);
		
		List authOperMenuList = sessionObject.getAuthOperMenuList();
		List list=queryResult.getResultList();
		List tmpList = new ArrayList();
		for(int i = 0; i < list.size(); i++) {
			TSysMenu sysMenu = (TSysMenu)list.get(i);
			if(authOperMenuList.contains(sysMenu.getMenuId())) {
				tmpList.add(sysMenu);
			}
		}
		try {
			BeanUtil.listToDataset(tmpList, dataset, false);
		} catch (Exception e) {
			e.printStackTrace();
			this.hintErrorMessage("加载菜单出错。");
		}
	}
}